#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>

//https://leetcode.cn/problems/linked-list-cycle-ii/submissions/
struct ListNode {
	int val;
	struct ListNode* next;

};
struct ListNode* detectCycle(struct ListNode* head) {
	if (!head)
		return NULL;
	struct ListNode* slow = head, * fast = head;
	struct ListNode* meet = head;
	struct ListNode* cur = head;

	while (slow && fast)
	{
		if (fast->next)
		{
			fast = fast->next->next;
			slow = slow->next;
			if (slow == fast)
			{
				meet = slow;
				while (meet != cur)
				{
					cur = cur->next;
					meet = meet->next;
				}
				return meet;
			}
		}
		else
			return NULL;

	}
	return NULL;
}